<?php
/**
 * Created by PhpStorm.
 * User: zhangfulai
 * Date: 2018/10/30
 * Time: 上午10:46
 */
namespace app\reserve\model;

use think\Model;

class Washcash extends Model{

    protected $hidden = ['a_id'];
    protected function initialize()
    {
        //需要调用`Model`的`initialize`方法
        parent::initialize();
        //TODO:自定义的初始化
    }
    protected function getCtimeAttr($vlue){
        return date("Y-m-d H:i:s",$vlue);
    }
    protected function getUtimeAttr($vlue){
        return date("Y-m-d H:i:s",$vlue);
    }
    protected function getLastLoginTimeAttr($vlue){
        return date("Y-m-d H:i:s",$vlue);
    }
    /*
    * Notes: 添加商户
    * Create Date: 2018/10/27
    * Create Time: 15:39
    * Create by :张福来
    * Param :
    */
    public function addWashcash($addarray){
        $reslt = $this->save($addarray);
        if($reslt){
            return $this->washid;
        }else{
            return false;
        }
    }
    public function editWashcash($addarray){
        $washid = $addarray['washid'];
        unset($addarray['washid']);
        $reslt = $this->allowField(true)->save($addarray,['washid'=>$washid]);
        return $reslt;
    }
    public function deleteWashcash($washid){
        $reslt = $this
            ->where(["washid"=>$washid])
            ->update(["status"=>9]);
        return $reslt;
    }
    public function selectWashcash($pageNum,$listNum,$w){
        $reslt["list"] = $this
            ->alias("c")
            ->field("c.washid,c.account,c.contactnum,c.province,c.city,c.area,c.address,c.lon,c.lat,c.last_login_time,c.last_login_ip,c.legal_person,
            c.	id_number,c.email,c.content,c.pic,c.name,c.wallet,c.score,c.a_id,c.allowradio,c.attestation_id,c.ctime,c.utime,c.status,
            a.area_name province_name,v.area_name city_name,x.area_name area_name,m.name cash_name")
            ->where($w)
            ->join("__AREAS__ a","c.province = a.area_id")
            ->join("__AREAS__ v","c.city = v.area_id")
            ->join("__AREAS__ x","c.area = x.area_id")
            ->join("__CASH__ m","c.a_id = m.a_id")
            ->page($pageNum,$listNum)
            ->order("c.ctime desc")
            ->select();
        $reslt["attestation"] = $this
            ->query("SELECT C.washid, GROUP_CONCAT(A. `NAME`) att_name,GROUP_CONCAT(A. pic) pic_path FROM orange_washcash C LEFT JOIN orange_attestation A ON FIND_IN_SET(A.id, C.attestation_id) GROUP BY C.washid");

        $reslt["total"] = $this
            ->alias("c")
            ->where($w)
            ->count("washid");
        return $reslt;

    }
    /**
     * Notes: 查找商户信息
     * Create Date: 2018/10/2
     * Create Time: 15:34
     * Create by :张福来
     */
    public function findCash($w){

        $result = $this->where($w)->find();
        return $result;
    }

    /**
     * Notes:  附近的洗车商户
     * Create Date: 2018/10/10
     * Create by :lijie
     */
    public function near($where,$pagenum,$listNum,$lat,$lnt)
    {
        $res = $this
            ->alias("w")
            ->where($where)
            ->field("w.washid wash_id,w.name wash_name,w.score wash_score,w.lon lng,w.lat,w.contactnum tel,w.address,w.pic,p.area_name province_name,c.area_name city_name,a.area_name area_name,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($lat*PI()/180-lat*PI()/180)/2),2)+COS($lat*PI()/180)*COS(lat*PI()/180)*POW(SIN(($lnt*PI()/180-lon*PI()/180)/2),2)))*1000) AS distance")
            ->join("__AREAS__ p"    ,"w.province = p.area_id" ,"LEFT")
            ->join("__AREAS__ c"    ,"w.city = c.area_id"     ,"LEFT")
            ->join("__AREAS__ a"    ,"w.area = a.area_id"     ,"LEFT")
            ->order("w.ctime desc")->page($pagenum,$listNum)->select();
        return $res;
    }

    /**
     * Notes:  根据手机号修改洗车商户密码
     * Create Date: 2018/10/17
     * Create by :lijie
     */
    public function editWashByMobile($param,$where)
    {
        $res = $this->isUpdate(true)->allowField(true)->save($param,$where);
        return $res;
    }

    /**
     * Notes:  洗车商户分数
     * Create Date: 2018/10/22
     * Create by :lijie
     */
    public function scoreWash($where)
    {
        return $this->where($where)->field("score")->find();
    }
}